package components.ghost;

import images.Images;

import java.awt.Color;
import java.util.List;

import ai.Pathfinder;

import com.uqbar.vainilla.DeltaState;
import com.uqbar.vainilla.appearances.Appearance;
import com.uqbar.vainilla.appearances.Circle;
import com.uqbar.vainilla.appearances.Sprite;
import components.PacmanComponent;
import components.Walker;
import components.direction.Direction;
import components.pacman.Pacman;
import components.scenes.MapMatrix;
import components.scenes.Slot;

public class Ghost extends Walker {


	private List<Slot> currentPath;
	private Walker target;
	private GhostState state;
	private Pathfinder pathfinder;
	private double chasingSpeed;
	private double scapingSpeed;
	
	public Ghost(Walker target) {
		super(20, 20, 0, 0);
		this.setTarget(target);
		this.state = new ChasingState(this);
		this.setPathfinder(Pathfinder.instance);
		this.setChasingSpeed(80);
		this.setScapingSpeed(30);
	}
	
	public Slot getTargetSlot() {
		return new Slot(this.getTarget().getCurrentX(), this.getTarget().getCurrentY());
	}
	
	public void updateDirection() {
		super.updateDirection();
		this.setDir(this.findNextDirection());
	}

	private Direction findNextDirection() {
		List<Slot> path = this.findPath();
		if (path == null){
			//throw new RuntimeException("path no encontrado");
			return this.getDir();
		}
		else{
			return this.getState().calculateNextDir(path.get(0));
		}
		
	}

	public List<Slot> findPath(){
		this.setCurrentPath(getPathfinder().findPath(this.getMapMatrix(), new Slot(this.getCurrentX(), this.getCurrentY()), this.getTargetSlot()));
		return this.getCurrentPath();
	}

	private MapMatrix getMapMatrix() {
		return this.getScene().getMapMatrix();
	}


	public void effectiveUpdate(DeltaState delta){
		super.effectiveUpdate(delta);
		this.getState().update(delta);
	}


	public Walker getTarget() {
		return target;
	}


	public void setTarget(Walker target) {
		this.target = target;
	}
	public void onCollisionWithPacman(Pacman pacman){
		//this.getScene().respawnPacman(pacman);
		this.getState().onCollisionWithPacman(pacman);
	}
	protected void defineCollide(PacmanComponent gc) {
		gc.onCollisionWithGhost(this);
	}


	@Override
	public String appRootName() {
		return "ghost";
	}
	
	public Sprite getChasingSprite(){
		return Images.getImage("ghost");
	}
	public Sprite getScapingSprite(){
		return Images.getImage("scaping-ghost");
	}

	public GhostState getState() {
		return state;
	}

	public void setState(GhostState state) {
		this.state = state;
	}

	public List<Slot> getCurrentPath() {
		return currentPath;
	}

	public void setCurrentPath(List<Slot> currentPath) {
		this.currentPath = currentPath;
	}

	public Pathfinder getPathfinder() {
		return pathfinder;
	}

	public void setPathfinder(Pathfinder pathfinder) {
		this.pathfinder = pathfinder;
	}

	public void becomeEatable(int seconds) {
		this.getState().scaping(seconds);
	}
	public double getSpeed(){
		return this.getState().getSpeed();
	}

	public void die() {
		this.getState().chasing();
		this.getScene().addScore(500);
		this.getScene().respawnGhost(this);
	}
	public Appearance getAppearance(){
		return this.getState().getAppaearance();
	}

	public double getChasingSpeed() {
		return chasingSpeed;
	}

	public void setChasingSpeed(double chasingSpeed) {
		this.chasingSpeed = chasingSpeed;
	}

	public double getScapingSpeed() {
		return scapingSpeed;
	}

	public void setScapingSpeed(double scapingSpeed) {
		this.scapingSpeed = scapingSpeed;
	}
}


